package com.java.huxb.reentrantlock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author huxb
 * @date 2020/4/10 0010 下午 2:55
 */
public class ReentrantLockTest {
    public static Lock lock = new ReentrantLock();

    public void buyTicket() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "正在买票！");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "买票结束！");
        } finally {
            lock.unlock();
        }

    }

    public static void main(String[] args) {
        new Thread(() -> new ReentrantLockTest().buyTicket()).start();
        new Thread(() -> new ReentrantLockTest().buyTicket()).start();
        new Thread(() -> new ReentrantLockTest().buyTicket()).start();
        new Thread(() -> new ReentrantLockTest().buyTicket()).start();
    }
}
